<template>
  <!-- 礼物表 -->
  <div class="app-container">
    <div class="app-wrap">
      <search-table ref="table" :table-loading="listLoading" :table-data="giftDataList" @query="getGiftList(true)" @reset="refresh">
        <template v-slot:row>
          <el-form-item label="游戏类型:" label-width="90px">
            <el-select v-model="listQuery.game_id" placeholder="请选择游戏类型" filterable >
              <el-option label="公共数据" :value="0"></el-option>
              <el-option :label="item.label" :value="item.value" v-for="(item, index) in GAME_TYPE" :key="index"></el-option>
            </el-select>
          </el-form-item>
        </template>
        <template v-slot:actions>
          <el-button type="primary" v-permission="'gift.add'" size="mini" icon="el-icon-plus" 
          @click="(isShowDetails = true),(checkedItem = null)">新增</el-button>
          <el-button type="info" size="mini" icon="el-icon-refresh" @click="getGiftList">刷新</el-button>
        </template>
        <template>
          <el-table-column align="center" type="index" width="50">
            <template slot-scope="scope">
              <el-radio v-model="scope.row.isChecked" :label="true" @change="handleCurrentChange(scope.row)">{{ "" }}</el-radio>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" prop="id" label="ID" width="80" /> -->
          <el-table-column align="center" prop="game_id" label="游戏类型" width="150">
            <template slot-scope="scope">
              <span>{{ gameType(scope.row.game_id) }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_base_gift_id" label="礼物ID" width="120" />
          <el-table-column align="center" prop="a_gift_remark" label="备注" />
          <el-table-column align="center" prop="a_gift_icon_name" label="调用图标资源" width="200" />
          <el-table-column align="center" prop="is_move" label="是否移动" width="120">
            <template slot-scope="scope">
              <el-tag :type="scope.row.is_move==1?'success':'info'" >{{ scope.row.is_move==1?'移动':'不移动' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_gift_type" label="礼物类型" width="120" >
            <template slot-scope="scope">
              <el-tag :type="scope.row.a_gift_type==1?'success':'info'" >{{ scope.row.a_gift_type==1?'好礼物':'坏礼物' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_cost_type" label="购买货币类型" width="120" >
            <template slot-scope="scope">
              <span>{{ gameCurrencyType(scope.row.a_cost_type) }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_cost_num" label="购买货币数量" width="150" />
          <el-table-column align="left" prop="operation" label="操作" width="200">
            <template slot-scope="scope">
              <el-tooltip effect="dark" v-permission="'gift.edit'" content="编辑" placement="top-start">
                <el-button size="mini" type="warning" @click.stop="editItem(scope.row)"><i class="el-icon-edit-outline" /></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" v-permission="'gift.delete'" content="删除" placement="top-start">
                <el-button size="mini" type="danger" @click.stop="deleteItem(scope.row)"><i class="el-icon-delete" /></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </template>
        <template v-slot:footer>
          <pagination  :total="currentTotal" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getGiftList"/>
        </template>
      </search-table>
      <page-details :show="isShowDetails" @onHideDetails="onHideDetails" :data="checkedItem" :propData="propDataList" :gameType="listQuery.game_id"/>
    </div>
  </div>
</template>

<script>
import { throttle } from "@/utils/index";
import { 
  getGift,
  delGift,
  getProp
 } from "@/api/game";
 import {
  GAME_TYPE,
  GAME_CURRENCY_TYPE
} from '@/utils/enum'
import PageDetails from "./page-details.vue";
import SearchTable from "@/components/SearchTable";

export default {
  name: "gift",
  components: {
    PageDetails,
    SearchTable,
  },
  data () {
    return {
      GAME_TYPE,
      GAME_CURRENCY_TYPE,
      giftDataList: [],
      checkedItem: null,
      listLoading: false,
      isShowDetails: false,
      currentTotal: 0,
      listQuery: {
        page: 1,
        size: 50,
        game_id:0
      },
      propDataList:[]
    };
  },
  created () {
    // this.getPropList()
  },
  activated () {
    this.getPropList()
  },
  methods: {
    // 获取礼物表
    async getGiftList (query) {
      let that = this,params=that.listQuery;
      if(query && !query.page){
        params.page=1
        params.size=50
      }
      that.giftDataList = [];
      that.checkedItem = null;
      that.listLoading = true;
      const response = await getGift(params)
      if (response.code==1) {
        if (response.data.list) {
          response.data.list.forEach((item) => {
            item.isChecked = false;
            item.a_cost_num=that.$public.mathNumberDivideWan(item.a_cost_num)
          });
          that.currentTotal = response.data.total;
          that.giftDataList = response.data.list;
        }
      }else{
        that.$message.error(response.msg)
      }
      that.listLoading = false;
    },
    // 获取货币类型
    async getPropList () {
      let that = this;
      that.propDataList = [];
      const response = await getProp({
        page:1,
        size:50,
        a_item_type:1,
        game_id:that.listQuery.game_id
      })
      if (response.code==1) {
        if (response.data.list) {
          response.data.list.forEach((item) => {
            that.propDataList.push({
              label:item.a_item_remark,
              value:item.a_base_item_id,
            })
          });
        }
      }else{
        that.$message.error(response.msg)
      }
      that.getGiftList()
    },
    // 游戏类型展示
    gameType(cellValue){
      if(cellValue){
        return (this.GAME_TYPE.find((item) => item.value === cellValue)?.label || "-");
      }else{
        return '公共数据'
      }
    },
    // 游戏货币类型展示
    gameCurrencyType(cellValue){
      return (this.propDataList.find((item) => item.value === cellValue)?.label || "-");
    },
    // 编辑
    editItem (row) {
      let that = this;
      that.isShowDetails = true;
      that.checkedItem = row;
      that.handleCurrentChange(row);
    },
    // 删除
    deleteItem (row) {
      let that = this;
      that.handleCurrentChange(row);
      that.$confirm(`是否删除【${row.a_gift_remark}】礼物配置`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
          delGift({
            id: row.id,
          }).then(function (res) {
            that.getGiftList();
            that.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => { });
    },
    // 选择表格数据
    handleCurrentChange (val) {
      this.checkedItem = val;
      this.giftDataList.map((item) => {
        if (item.id === val.id) {
          item.isChecked = true;
        } else {
          item.isChecked = false;
        }
      });
    },
    // 隐藏详情
    onHideDetails (show) {
      this.isShowDetails = false;
      if (show) {
        this.getGiftList();
      }
    },
    // 刷新
    refresh: throttle(function () {
      this.listQuery = {
        page: 1,
        size: 50,
        game_id:0
      };
      this.getGiftList();
    }, 2000),
  },
};
</script>

<style lang="scss" scoped></style>
